package com.vanke.house.api.response;

import com.vanke.house.utils.DateUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 封装统一返回的JSON Response
 * @author fangyu
 * @version v1.0.0
 * @date 2020/5/7 10:31 上午
 */
@Data
@NoArgsConstructor
public class JsonResult<T> {
    /**
     * code 状态码
     * message 提示消息
     * data 返回结果
     * time 请求时间
     */
    private Integer code;
    private String message;
    private T data;
    private String time = DateUtils.newTime();

    public JsonResult(ResultCode resultCode, T data){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    public JsonResult(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public JsonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 200 成功返回
     * @return { code: 200, message: "操作成功" }
     */
    public static <T> JsonResult<T> success() {
        return new JsonResult<T>(ResultCode.SUCCESS, null);
    }

    /**
     * 200 成功返回
     * @param data 获取的数据
     * @return { code: 200, message: "操作成功", data: data }
     */
    public static <T> JsonResult<T> success(T data) {
        return new JsonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }


    /**
     * 200 成功返回
     * @param message 返回msg
     * @param data 获取的数据
     * @return { code: 200, message: message, data: { key: value } }
     */
    public static <T> JsonResult<T> success(T data, String message) {
        return new JsonResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 自定义错误返回信息
     * @param errorCode 错误码
     */
    public static JsonResult failed(IErrorCode errorCode) {
        return new JsonResult(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 500 自定义message
     * @return { code: 500, message: message }
     */
    public static JsonResult failed(String message) {
        return new JsonResult(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 500 操作失败
     * @return { code: 500, message: "操作失败" }
     */
    public static JsonResult failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 404 参数校验失败
     * @return { code: 404, message: "参数校验失败" }
     */
    public static JsonResult validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 404 自定义message
     * @return { code: 404, message: message }
     */
    public static JsonResult validateFailed(String message) {
        return new JsonResult(ResultCode.VALIDATE_FAILED.getCode(), message);
    }

    /**
     * 401 暂未登录或token已经过期
     * @return { code: 401, message: "暂未登录或token已经过期" }
     */
    public static JsonResult unauthorized(){
        return new JsonResult(ResultCode.UNAUTHORIZED);
    }

    /**
     * 403 没有相关权限
     * @return { code: 403, message: "没有相关权限" }
     */
    public static JsonResult forbidden(){
        return new JsonResult(ResultCode.FORBIDDEN);
    }
}
